//
//  NameViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/8/4.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "NameViewController.h"

@interface NameViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@end

@implementation NameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.titleName;
    
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn addTarget:self action:@selector(saveBtnItemAction) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn sizeToFit];
    UIBarButtonItem *saveBtnItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    
    self.navigationItem.rightBarButtonItem = saveBtnItem;
}

#pragma mark -- 保存
- (void)saveBtnItemAction {

    [self requestModiuser];
}

#pragma mark -- 修改用户信息
- (void)requestModiuser {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *userid = [[NSUserDefaults standardUserDefaults] objectForKey:kUserid];
    dict[@"command"] = @"modiuser";
    dict[@"userid"] = userid;
    dict[@"nickname"] = self.nameTextField.text;

    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"修改用户信息 = %@",responseObject);
            [[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:kNickname];
            [self.navigationController popViewControllerAnimated:YES];
            if ([_delegate respondsToSelector:@selector(nameViewControllerWithString:)]) {
                [_delegate nameViewControllerWithString:self.nameTextField.text];
            }
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}


@end
